# -*- coding: latin-1 -*-
import unittest
from django.test.client import Client
import datetime

from sirogi.documents.models import Document
from sirogi.states.models import State
from sirogi.users.models import SirogiUser

################################# Para testear las vistas
class DocViewTest(unittest.TestCase):
	fixtures = ['initial_data.xml']

# Test 1: los revisores no pueden insertar documentos
	def	test_add_a_doc_as_revisor(self):
		c = Client()

		# Logueo al revisor
		self.failUnlessEqual(c.login(username='revisor', password='revisor'), True)

		# llamada
		response = c.get('/documents/write_document/')

		# 200 http response
		self.failUnlessEqual(response.status_code, 200)

		# Que sea la pagina correcta la que muestra
		self.failUnlessEqual("Write Document" in response.content, True)

		# Escribo contenido al documento
		#import pdb; pdb.set_trace()
		response = c.post('/documents/write_document/save/',{'number': 20, 'date': '2008-11-18 20:15:12.294318', 'visto':'visto visto', 'consider': 'consider consider', 'resolv': 'resolv resolv'})

		#import pdb; pdb.set_trace()
		self.failUnlessEqual("Go back to login." in response.content, True)
		self.failUnlessEqual(response.status_code, 200)

# Test 2: los invitados no pueden insertar documentos
	def	test_add_a_doc_as_invitado(self):
		c = Client()

		# Logueo al revisor
		self.failUnlessEqual(c.login(username='invitado', password='invitado'), True)

		# llamada
		response = c.get('/documents/write_document/')

		# 200 http response
		self.failUnlessEqual(response.status_code, 200)

		# Que sea la pagina correcta la que muestra
		self.failUnlessEqual("Write Document" in response.content, True)

		# Escribo contenido al documento
		#import pdb; pdb.set_trace()
		response = c.post('/documents/write_document/save/',{'number': 20, 'date': '2008-11-18 20:15:12.294318', 'visto':'visto visto', 'consider': 'consider consider', 'resolv': 'resolv resolv'})

		#import pdb; pdb.set_trace()
		self.failUnlessEqual("Go back to login." in response.content, True)
		self.failUnlessEqual(response.status_code, 200)

# Test 3: un redactor puede insertar documentos
	def	test_add_a_doc_as_redactor(self):
		c = Client()

		# Logueo al revisor
		self.failUnlessEqual(c.login(username='redactor', password='redactor'), True)

		# llamada
		response = c.get('/documents/write_document/')

		# 200 http response
		self.failUnlessEqual(response.status_code, 200)

		# Que sea la pagina correcta la que muestra
		self.failUnlessEqual("Write Document" in response.content, True)

		# Escribo contenido al documento
		response = c.post('/documents/write_document/save/',{'number': 20, 'date': '2008-11-18 20:15:12.294318', 'visto':'visto visto', 'consider': 'consider consider', 'resolv': 'resolv resolv'})

		#import pdb; pdb.set_trace()
		self.failUnlessEqual("Saved OK. Click to return." in response.content, True)
		self.failIfEqual("Go back to login." in response.content, True)
		self.failUnlessEqual(response.status_code, 200)

# Test 4: un redactor puede editar un documento que él mismo creó
	def	test_edit_doc_as_redactor_owner(self):
		c = Client()

		# Logueo al revisor
		self.failUnlessEqual(c.login(username='redactor', password='redactor'), True)

		# llamada
		response = c.get('/documents/edit_document/30/')
		
		# Que sea la pagina correcta la que muestra
		self.failUnlessEqual("En Redaccion"  in response.content, True)
		self.failUnlessEqual("Edit Document" in response.content, True)

		# 200 http response
		self.failUnlessEqual(response.status_code, 200)

		# Escribo contenido al documento
		date = datetime.datetime.now()
		response = c.post('/documents/edit_document/30/save2/',{'date': date, 'visto':'visto visto', 'consider': 'consider consider', 'resolv': 'resolv resolv'})
		
		self.doc  = Document.objects.get(numDoc=30)
		# A ver si se guardaron bien los datos:
		self.failUnlessEqual(self.doc.Get_numDoc(), 30)
		self.failUnlessEqual(self.doc.Get_visto(), 'visto visto')
		self.failUnlessEqual(self.doc.Get_considerando(), 'consider consider')
		self.failUnlessEqual(self.doc.Get_resuelv(), 'resolv resolv')

		#import pdb; pdb.set_trace()
		self.failUnlessEqual("Saved OK. Click to return." in response.content, True)
		self.failIfEqual("Go back to login." in response.content, True)
		self.failUnlessEqual(response.status_code, 200)


# Test 5: un redactor no puede editar un documento que él mismo no creó
	def	test_edit_doc_as_redactor_not_owner(self):
		c = Client()

		# Logueo al revisor
		self.failUnlessEqual(c.login(username='redactor2', password='redactor2'), True)

		# llamada
		response = c.get('/documents/edit_document/30/')
		
		# Que sea la pagina correcta la que muestra
		self.failUnlessEqual("En Redaccion"  in response.content, True)
		self.failUnlessEqual("Edit Document" in response.content, True)

		# 200 http response
		self.failUnlessEqual(response.status_code, 200)

		# Escribo contenido al documento
		date = datetime.datetime.now()
		response = c.post('/documents/edit_document/30/save2/',{'date': date, 'visto':'visto visto', 'consider': 'consider consider', 'resolv': 'resolv resolv'})
		
		self.doc  = Document.objects.get(numDoc=30)
		# A ver si se guardaron bien los datos:
		self.failUnlessEqual(self.doc.Get_numDoc(), 30)
		self.failIfEqual(self.doc.Get_visto(), 'visto visto')
		self.failIfEqual(self.doc.Get_considerando(), 'consider consider')
		self.failIfEqual(self.doc.Get_resuelv(), 'resolv resolv')

		#import pdb; pdb.set_trace()
		self.failIfEqual("Saved OK. Click to return." in response.content, True)
		self.failUnlessEqual("Go back to login." in response.content, True)
		self.failUnlessEqual(response.status_code, 200)


# Test 6: editar un documento cancelado
	def	test_edit_a_canceled_doc(self):
		c = Client()

		# Logueo al revisor
		self.failUnlessEqual(c.login(username='redactor', password='redactor'), True)

		# llamada
		response = c.get('/documents/edit_document/1/')
		
	# Por las vias normales (clickeando links de la página)
		# Que sea la pagina correcta la que muestra
		self.failUnlessEqual("Document state is Canceled. Cannot be edited or sent to review."  in response.content, True)

		# 200 http response
		self.failUnlessEqual(response.status_code, 200)

	# Por la vía "no normal": intentando mandar a editar escribiendo en la barra de dirección la llamada a save2
		# Escribo contenido al documento
		date = datetime.datetime.now()
		response = c.post('/documents/edit_document/1/save2/',{'date': date, 'visto':'visto visto', 'consider': 'consider consider', 'resolv': 'resolv resolv'})
		
		self.doc  = Document.objects.get(numDoc=1)
		# A ver si se guardaron los datos:
		self.failUnlessEqual(self.doc.Get_numDoc(), 1)
		self.failIfEqual(self.doc.Get_visto(), 'visto visto')
		self.failIfEqual(self.doc.Get_considerando(), 'consider consider')
		self.failIfEqual(self.doc.Get_resuelv(), 'resolv resolv')

		#import pdb; pdb.set_trace()
		self.failIfEqual("Saved OK. Click to return." in response.content, True)
		self.failUnlessEqual("Document state is Canceled. Cannot be edited or sent to review." in response.content, True)
		self.failUnlessEqual(response.status_code, 200)


# Test 7: enviar a revision un documento cancelado
	def	test_send_to_review_a_canceled_doc(self):
		c = Client()

		# Logueo al revisor
		self.failUnlessEqual(c.login(username='redactor', password='redactor'), True)

		# llamada
		response = c.get('/documents/send_to_rev/1/')
		
		# Que sea la pagina correcta la que muestra
		self.failUnlessEqual("Document state is Canceled. Cannot be edited or sent to review."  in response.content, True)

		# 200 http response
		self.failUnlessEqual(response.status_code, 200)

		self.doc  = Document.objects.get(numDoc=1)
		stCancel = State()
		stCancel = State.objects.get(Name='C')
		stE = State()
		stE = State.objects.get(Name='E')
		# A ver si se guardaron los datos:
		self.failUnlessEqual(self.doc.Get_state(), stCancel)
		self.failIfEqual(self.doc.Get_state(), stE)

		#import pdb; pdb.set_trace()
		self.failIfEqual("Saved OK. Click to return." in response.content, True)
		self.failUnlessEqual("Document state is Canceled. Cannot be edited or sent to review." in response.content, True)
		self.failUnlessEqual(response.status_code, 200)

# Test 8: editar un documento que no existe
	def	test_edit_nonexistent_doc(self):
		c = Client()

		# Logueo al revisor
		self.failUnlessEqual(c.login(username='redactor', password='redactor'), True)

		# llamada
		response = c.get('/documents/edit_document/150/')
		
		# Que sea la pagina correcta la que muestra
		self.failUnlessEqual("No documents with this document number."  in response.content, True)

		# 200 http response
		self.failUnlessEqual(response.status_code, 200)

		# Escribo contenido al documento
		date = datetime.datetime.now()
		response = c.post('/documents/edit_document/150/save2/',{'date': date, 'visto':'visto visto', 'consider': 'consider consider', 'resolv': 'resolv resolv'})
		
		#import pdb; pdb.set_trace()
		self.failIfEqual("Saved OK. Click to return." in response.content, True)
		self.failUnlessEqual("No documents with this document number." in response.content, True)
		self.failUnlessEqual(response.status_code, 200)















################################# Para testear el model
class DocModelTest(unittest.TestCase):


	def setUp(self):
		st = State.objects.get(Name = 'E')
		ow = SirogiUser.objects.get(NIC = '1234')
		self.doc = Document.objects.create(owner  = ow, state = st, numDoc	= 10, date = '2008-11-18 20:15:12.294318', visto = 'visto visto', considerando = 'considerando considerando', resuelve = 'resuelve resuelve')
		#sin path ni colaboradores
	
	def testCreating(self):
		st = State.objects.get(Name = 'E')
		ow = SirogiUser.objects.get(NIC = '1234')
		
		self.failUnlessEqual(self.doc.Get_Owner(),ow)
		self.failUnlessEqual(self.doc.Get_state(),st)
		self.failUnlessEqual(self.doc.Get_numDoc(), 10)
		self.failUnlessEqual(self.doc.Get_visto(), 'visto visto')
		self.failUnlessEqual(self.doc.Get_considerando(), 'considerando considerando')
		self.failUnlessEqual(self.doc.Get_resuelv(), 'resuelve resuelve')
		self.failUnlessEqual(self.doc.Get_date(), '2008-11-18 20:15:12.294318')